<template>
  <div>
      <page-header :title="'毕业生 ' + title" :content="'graduates ' + type" />
      <page-main>
          <el-row>
              <el-col :span="24" :lg="12">
                  <el-form ref='form' :model="form" label-width="120px">
                      <el-form-item label="姓名">   
                        <el-input v-model="form.name" :disabled="type == 'view'"/>
                      </el-form-item>
                      <el-form-item label="学号">
                        <el-input v-model="form.stuno" :disabled="type == 'view'"/>
                      </el-form-item>
                      <el-form-item label="班级">
                        <el-input v-model="form.clazz" :disabled="type == 'view'"/>
                      </el-form-item>
                      <el-form-item label="所属专业">
                        <!-- <el-input v-model="form.professionname" :disabled="type == 'view'"/> -->
                        <el-select v-model="form.profession" placeholder="请选择" :disabled="type == 'view'">
                            <el-option
                            v-for="item in professionList"
                            :key="item.value"
                            :label="item.professionname"
                            :value="item.id">
                            </el-option>
                        </el-select>
                      </el-form-item>
                      <el-form-item label="学历">
                        <el-input v-model="form.education" :disabled="type == 'view'"/>
                      </el-form-item>
                      <el-form-item label="毕业年份">
                        <el-input v-model="form.graduateyear" :disabled="type == 'view'"/>
                      </el-form-item>
                      <el-form-item label="薪资">
                        <el-input v-model="form.salary" :disabled="type == 'view'"/>
                      </el-form-item>
                      <el-form-item label="">
                        <el-button type="primary" v-if="type != 'view'" @click="update()">确定</el-button>
                        <el-button @click="cannelBuild()">取消</el-button>
                      </el-form-item>
                  </el-form>
              </el-col>
          </el-row>
      </page-main>
  </div>
</template>

<script>
export default {
    name: 'graduates_update',
    data() {
        return {
            type: '',
            title: '',
            id: '',
            form: {},
            professionList: []
        }
    },
    watch:{
        '$route.path': function(newVal,oldVal){
            //console.log(newVal+"---"+oldVal);
            console.log(newVal);
        }
    },
    beforeRouteEnter(to, from, next) {
        let that = this
        next(vm => {
            // 通过 `vm` 访问fetchData，将query参数name，传递过去，进行逻辑处理
            // that.type = vm.$route.params.type
            vm.fetchData(vm.$route.params.type, vm.$route.params.id);
        });
    },
    methods: {
        fetchData(type, id) {
            this.type = type
            let titleList = {
                add: '新增',
                edit: '编辑',
                view: '查看'
            }
            this.id = id
            this.title = titleList[type]
            if (id != '*') {
                this.$util.post('public/selectGraduatesByPrimaryKey', {id: id}).then(res => {
                    this.form = res
                })
            }

            this.$util.post('public/loadAllProfession').then(res => {
                this.professionList = []
                for(let i = 0; i < res.length; i++) {
                    this.professionList.push(res[i])
                }
            })
        },
        cannelBuild() {
            this.form = {}
            this.$router.back(-1)
        },
        update() {
            if (this.type == 'add') {
                this.$util.post('public/insertGraduates', this.form).then(res => {
                    this.$message({
                        message: '新增成功',
                        type: 'success'
                    });
                    this.$router.push('/graduates_manager/graduatesList')
                })
            } else {
                this.$util.post('public/updateGraduatesByPrimaryKey', this.form).then(res => {
                    this.$message({
                        message: '修改成功',
                        type: 'success'
                    });
                    this.$router.push('/graduates_manager/graduatesList')
                })
            }
        }
    }
}
</script>
